<script lang="ts" setup>
  import { getHandicapCourseList, bindHandicapCourse } from '@/api/course';
  import { onMounted, ref } from 'vue';
  import { HandicapCourse } from '@/types/course';
  import { Message } from '@arco-design/web-vue';

  const props = defineProps<{
    courseId: string;
    courseName: string;
  }>();

  const emit = defineEmits(['close']);

  const handicapCourseList = ref<HandicapCourse[]>([]);
  const searchStr = ref(props.courseName);
  const loading = ref(false);
  const selectedHandicapCourseId = ref('');

  const searchHandicapCourse = async () => {
    selectedHandicapCourseId.value = '';
    loading.value = true;
    try {
      const res = await getHandicapCourseList(searchStr.value);
      handicapCourseList.value = res.data.data.data?.items || [];
    } finally {
      loading.value = false;
    }
  };

  const saving = ref(false);
  const handleSubmit = async () => {
    if (!selectedHandicapCourseId.value) {
      Message.warning('请选择要绑定的球场');
      return;
    }

    try {
      saving.value = true;
      await bindHandicapCourse(props.courseId, selectedHandicapCourseId.value);
      Message.success('绑定成功');
      emit('close', true);
    } finally {
      saving.value = false;
    }
  };

  onMounted(() => {
    searchHandicapCourse();
  });
</script>

<template>
  <a-modal
    title="绑定差点系统"
    :width="700"
    visible
    :ok-loading="saving"
    @cancel="emit('close', false)"
    @before-ok="handleSubmit"
  >
    <a-row :gutter="[20, 20]">
      <a-col>
        <a-input-search
          v-model="searchStr"
          placeholder="搜索要绑定的球场"
          @search="searchHandicapCourse"
        />
      </a-col>
      <a-col>
        <div
          style="
            padding: 0 12px;
            font-size: 12px;
            color: var(--color-text-2);
            margin-bottom: 5px;
          "
        >
          选择要绑定的球场：
        </div>
        <div class="list">
          <div
            v-for="course in handicapCourseList"
            :key="course.id"
            class="item"
            :class="{ active: course.id === selectedHandicapCourseId }"
            @click="selectedHandicapCourseId = course.id"
          >
            <div class="item-name">{{ course.name }}</div>
            <div
              v-if="course.id === selectedHandicapCourseId"
              style="color: rgb(var(--green-6))"
            >
              <icon-check-circle-fill />
            </div>
          </div>
        </div>
      </a-col>
    </a-row>
  </a-modal>
</template>

<style lang="less">
  .list {
    max-height: 400px;
    overflow: auto;
  }
  .item {
    display: flex;
    align-items: center;
    height: 36px;
    cursor: pointer;
    padding-right: 12px;
    &:hover {
      background: rgb(var(--gray-2));
    }
    &.active {
      background: rgb(var(--primary-1));
    }
    &-name {
      flex: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      padding: 0 12px;
    }
  }
</style>
